<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>

        //实现深拷贝的三种方式
     let obj1 = {
         a: 1,
         b:'小明',
        c:{
            name:'小红',
            say:function (){
             console.log('我是'+this.name);
         },
        },
         d:undefined
     }
     let obj2 =JSON.parse(JSON.stringify(obj1))
     console.log(obj2);
    
     let obj3 = Object.assign(obj1);
     obj3.c.name = '小白'
     console.log(obj1,obj3);
     console.log(obj3);

     function deepCopy(obj){
         let res = obj
         if (typeof obj === 'object' && obj!==null) {
         res = Object.prototype.toString.call(obj) ==='[object Object]'?{}:[]
         for(let prop in obj){
             res[prop]= deepCopy(obj[prop])
         } 
         } 
        
         return res
     }

      
     let obj4 = deepCopy(obj1)
     obj4.c.name = '小白'
     console.log(obj1,obj4);




     //实现鉴别类型

     function type(obj){
         if (typeof obj === null) {
             return null
         }else if (typeof obj === 'object') {
             if (Array.isArray(obj)) {
                 return Array
             }else{
                 return typeof obj
             }        
         }else if (typeof obj === 'number') {
            if (isNaN(obj)) {
                return NaN
            } else{
                return typeof obj
            }
         }else{
             return typeof obj
         }
     }

     function type2(obj){
         return typeof obj
     }

     let a = NaN;
     console.log(type(a));
     console.log(type2(a));


    // 数组去重

    function disarr(arr){
        let newArr = [];
      for(let i in arr){
          if (newArr.indexOf(arr[i]) == -1) {
              newArr.push(arr[i])
          }
      }
      return newArr
    }


    let arr = [1,2,3,4,5,5,6,6,7,7,1];
    let setCom = Array.from(new Set(arr))
    console.log(disarr(arr),setCom);



  //使用ajax封装接口

 
   function getData(url){
       let xhr = new XMLHttpRequest()
       console.log(xhr);
       xhr.open('GET',url);
       xhr.send()
       xhr.onreadystatechange = function(){
           if (xhr.readyState == 4 && xhr.status == 200) {
               
               let result = JSON.parse(xhr.response);
               console.log(result.result);
           }else if(xhr.readyState !=4 && xhr.status != 200){
               console.log('调用失败');
           }
           return result
       }
      
       
   }
   let url = 'https://api.apiopen.top/getJoke?page=1&count=2&type=video';
   let data = getData(url);
console.log(data);


    </script>
</body>
</html>